package com.bgl.mall.common;

import com.bgl.mall.util.PropertiesUtil;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @author Bu Guoliang
 * @date 2019/07/06 12:27
 * @since 1.8
 */
public class RedisPool {
    /** jedis连接池 */
    private static JedisPool pool;
    /** 最大连接数 */
    private static Integer maxTotal = Integer.parseInt(PropertiesUtil.getProperty("redis.max.total", "20"));
    /** 在jedispool中最大的idle状态(空闲的)的jedis实例个数 */
    private static Integer maxIdle = Integer.parseInt(PropertiesUtil.getProperty("redis.max.idle", "10"));
    /** 在jedispool中最小的idle状态(空闲的)的jedis实例个数 */
    private static Integer minIdle = Integer.parseInt(PropertiesUtil.getProperty("redis.min.idle", "2"));
    /** 在borrow一个jedis实例时，是否需要验证操作，如果赋值true，则得到的jedis实例肯定是可以用的 */
    private static Boolean testOnBorrow = Boolean.parseBoolean(PropertiesUtil.getProperty("redis.test.borrow", "true"));
    /** 在return一个jedis实例时，是否需要验证操作，如果赋值true，则返回的jedis实例肯定是可以用的 */
    private static Boolean testOnReturn = Boolean.parseBoolean(PropertiesUtil.getProperty("redis.test.return", "true"));

    private static String redisIp = PropertiesUtil.getProperty("redis.ip");
    private static Integer redisPort = Integer.parseInt(PropertiesUtil.getProperty("redis.port"));

    private static void initPool(){
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(maxTotal);
        config.setMaxIdle(maxIdle);
        config.setMinIdle(minIdle);

        config.setTestOnBorrow(testOnBorrow);
        config.setTestOnReturn(testOnReturn);
        //连接耗尽时是否阻塞，false会抛出异常，true阻塞直到超时，默认为true
        config.setBlockWhenExhausted(true);

        pool = new JedisPool(config, redisIp, redisPort, 2*1000);
    }

    static {
        initPool();
    }

    public static Jedis getJedis(){
        return pool.getResource();
    }

    public static void close(Jedis jedis){
        jedis.close();
    }

    public static void main(String[] args) {
        Jedis jedis = getJedis();
        jedis.set("ads","dadr");
        close(jedis);
    }

}
